package com.itheima.utils;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MybatisUtil {

    private final static SqlSessionFactory factory;

    //加载会话工厂
    static {
        InputStream resourceAsStream = MybatisUtil.class.getResourceAsStream("/mybatis-config.xml");
        factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    }

    //获得会话
    public static SqlSession getSession(){
        SqlSession session = factory.openSession();
        return session;
    }

    //获得自动提交的会话
    public static SqlSession getAutoCommitSession(){
        SqlSession autoCommitSession = factory.openSession(true);
        return autoCommitSession;
    }
}
